package org.moonlight.jvm.instructions.base;

import org.moonlight.jvm.rtda.exclusive.Frame;

/**
 * 跳转指令，offset字段存放跳转偏移量。
 *
 * @author Moonlight
 * @createTime 2023/8/16 14:06
 **/
public abstract class BranchInstruction implements Instruction {

    /** 跳转偏移量 - 一般是以双字节数保存 **/
    protected int offset;

    @Override
    public void fetchOperands(BytecodeReader reader) {
        this.offset = reader.readInt16();
    }

    protected void branch(Frame frame) {
        branch(frame, this.offset);
    }

    protected void branch(Frame frame, int offset) {
        int pc = frame.getThread().getPc();
        frame.setNextPc(pc + offset);
    }
}
